using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Forecast.Business;
using Forecast.Database;

public delegate void OfferCreatedEventHandler(object sender, EventArgs e);

public partial class controls_misc_OfferEditor : System.Web.UI.UserControl
{
    public event OfferCreatedEventHandler OfferCreated;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Initialize(int predictionId)
    {
        ViewState["predictionId"] = predictionId;

        Load loader = new Load(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);

        Player player = loader.GetPlayer(UserHelper.GetUserId(Page.User.Identity.Name));
        Prediction prediction = loader.GetPrediction(predictionId);
        Holding holding = loader.GetHolding(player, prediction);
        
        int numPredictions = holding.NumPredictions;
        if (numPredictions > 0)
        {
            _numStocksSliderExtender.Maximum = numPredictions;
        }
        else
        {
            _splitter.Visible = false;
            _sellPanel.Visible = false;
        }
    }

    protected void _makeOfferButton_Click(object sender, EventArgs e)
    {        
        Forecast.Business.NewOfferParams offerParams = new Forecast.Business.NewOfferParams();
        offerParams.PredictionId = (int)ViewState["predictionId"];
        offerParams.Price = Int32.Parse(_price.Text);
        offerParams.PlayerId = UserHelper.GetUserId(Page.User.Identity.Name);
        offerParams.NumPredictions = Int32.Parse(_numStocks.Text);
        offerParams.IsBuy = false;
        offerParams.IsActive = true;
        offerParams.EndDate = DateTime.Parse(_endDate.Text, System.Globalization.CultureInfo.InvariantCulture);

        CreateOffer(offerParams);
    }

    protected void _makeOfferToBuyButton_Click(object sender, EventArgs e)
    {
        Forecast.Business.NewOfferParams offerParams = new Forecast.Business.NewOfferParams();
        offerParams.PredictionId = (int)ViewState["predictionId"];
        offerParams.Price = Int32.Parse(_priceToBuy.Text);
        offerParams.PlayerId = UserHelper.GetUserId(Page.User.Identity.Name);
        offerParams.NumPredictions = Int32.Parse(_numStocksToBuy.Text);
        offerParams.IsBuy = true;
        offerParams.IsActive = true;
        offerParams.EndDate = DateTime.Parse(_endDateToBuy.Text, System.Globalization.CultureInfo.InvariantCulture);

        CreateOffer(offerParams);
    }    
    
    private void CreateOffer(Forecast.Business.NewOfferParams offerParams)
    {
        Forecast.Business.Activity activity = new Forecast.Business.Activity(
            ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);

        activity.NewOffer(offerParams);

        _numStocks.Text = _numStocksToBuy.Text = "0";
        _endDate.Text = _endDateToBuy.Text = string.Empty;        

        if (OfferCreated != null)
        {
            OfferCreated(this, null);
        }
        Response.Redirect(ResolveUrl(String.Format("~/Prediction.aspx?id={0}", Convert.ToInt32(ViewState["predictionId"]))));
    }
}
